﻿@model  WaiGaoQiaoWeb.Models.ArticleVM
@{
    ViewBag.Title = "ArticleIndex";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Head
{
    @Html.Css("main", "flexigrid", "tree")
    <script type="text/javascript">
        var maiheight = document.documentElement.clientHeight;
        $(document).ready(function () {
            var mainWidth = document.documentElement.clientWidth - 253; // 减去边框和左边的宽度
            var otherpm = 133;
            var gh = maiheight - otherpm;
            var option = {
                height: gh,
                width: mainWidth,
                url: '@Url.Action("GetWork_ArticleList")',
                dataType: 'json',
                colModel: [
                    { display: '物资ID', name: 'ID', width: 50, sortable: false, hide: true, align: 'left', iskey: true },
                    { display: '物资编号', name: 'ArticleNO', width: 80, sortable: false, hide: false, align: 'left', iskey: true },
                    { display: '物资名称', name: 'ArticleName', width: 200, sortable: false, hide: false, align: 'left', iskey: true },
                    { display: '物资规格', name: 'ArticleUnit', width: 80, sortable: false, hide: false, align: 'left', iskey: true },
				    { display: '领取类型', name: 'PriorId', width: 80, sortable: false, hide: false, align: 'left', iskey: true },
                	{ display: '上级ID', name: 'GetType', width: 80, sortable: false, align: 'left', iskey: true },
                    { display: '物资颜色', name: 'Color', width: 80, sortable: false, hide: false, align: 'left' }
				
                ],
                buttons: [
						    { name: 'Add', displayname: "新建", title: "新建组织", bclass: 'add InsertClass', onpress: toolbarItem_onclick },
							{ name: 'Edit', displayname: "修改", title: "修改组织", bclass: 'edit UpdateClass', onpress: toolbarItem_onclick },
                            { name: 'Delete', displayname: "删除", title: "删除组织", bclass: 'delete DeleteClass', onpress: toolbarItem_onclick },
							{ separator: true }
						 ],
                sortname: "DispOrder",
                sortorder: "asc",
                title: false,
                singleselected: false,
                usepager: true,
                page: 1, //默认当前页  
                total: 1, //总页面数  
                useRp: true, //是否可以动态设置每页显示的结果数  
                rp: 20, // 每页默认的结果数  
                rpOptions: [10, 15, 20], //可选择设定的每页结果数
                showcheckbox: true,
                selectedonclick: true,
                rowbinddata: true
            };

            var grid = $("#ConGoodList").flexigrid(option);


            $("#ptree").treeview({
                url: '@Url.Action("QueryWork_Article")',
                showcheck: false,
                onnodeclick: TreeNode_Click,
                theme: "bbit-tree-no-lines" //bbit-tree-lines ,bbit-tree-no-lines,bbit-tree-arrows
            }).height(maiheight - 30);

        });

        function refresh() {
            $("#ConGoodList").flexReload();
            $("#ptree").treeview({
                url: '@Url.Action("QueryWork_Article")',
                showcheck: false,
                onnodeclick: TreeNode_Click,
                theme: "bbit-tree-no-lines"
            }).height(maiheight - 30);
        }


        // 触发树事件 
        function TreeNode_Click(data) {
            var p = { extParam: [{ name: "Key", value: data.id}] };
            $("#ConGoodList").flexOptions(p).flexReload();
            $("#PriorId").val(data.id);
        } // end of TreeNode_Click


        //新增，删除，修改
        function toolbarItem_onclick(cmd, grid) {
            if (cmd == "Add") {
                var id = $("#ID").val();
                var parentId = encodeURIComponent($("#PriorId").val());
                OpenModelWindow('@Url.Action("ArticleEdit")?Id=' + id + '&ParentId=' + parentId,
                    {
                        width: 350,
                        height: 250,
                        caption: "新增物资",
                        onclose: function () {
                            showErrorTip("操作成功！", { left: 400, top: 2 }, true, 3000);
                            refresh();
                        }
                    });
            }
            else if (cmd == "Edit") {
                if ($('.trSelected', grid).length == 0) {
                    showErrorTip("请选择要编辑的行！", { left: 400, top: 2 }, true, 3000);
                    return false;
                }

                if ($('.trSelected', grid).length > 1) {
                    showErrorTip("只能选择一行！", { left: 400, top: 2 }, true, 3000);
                    return false;
                }
                var parentId = encodeURIComponent($("#PriorId").val());
                var items = $('.trSelected', grid);
                var Id = items[0].id.substr(3);
                OpenModelWindow('@Url.Action("ArticleEdit")?Id=' + Id,
                                   { width: 350, height: 250, caption: "编辑物资",
                                       onclose: function () {
                                           showLoadingMsg("操作成功", { left: 400, top: 2 }, true, 3000);
                                           refresh();
                                       }
                                   });
            }
            else if (cmd = "Delete") {
                if ($('.trSelected', grid).length == 0) {
                    showErrorTip("请选择要删除的行！", { left: 400, top: 2 }, true, 3000);
                    return false;
                }

                var parentId = encodeURIComponent($("#PriorId").val());
                if (confirm("确定要删除吗?")) {
                    var ids = "";
                    var items = $('.trSelected', grid);
                    for (i = 0; i < items.length; i++) {
                        ids = items[i].id.substr(3);
                        if (IsexistsSubnode(ids) == true) {
                            showLoadingMsg("包含子物品,不能删除！", { left: 400, top: 2 }, true, 3000);
                            //alert(IsexistsSubnode(ids));
                        }
                        else {
                            $.ajax({
                                url: '@Url.Action("DeleteWork_Article")',
                                type: "POST",
                                data: { GoodsIds: ids },
                                success: function (callback) {
                                    if (callback) {
                                        if ((callback != true) && (callback != false)) {
                                            alert(callback);
                                            return;
                                        }
                                        showLoadingMsg("删除成功", { left: 400, top: 2 }, true, 3000);
                                        refresh();
                                    }
                                },
                                error: function () {
                                    showErrorTip("操作失败！", { left: 400, top: 2 }, true, 3000);
                                }
                            }); // end $.ajax
                        }
                    } //for                    
                }
            }
        }

        function IsexistsSubnode(pid) {
            var isexist = false;
            $.ajax({
                url: '@Url.Action("IsExistSubNode")',
                type: "POST",
                data: { id: pid },
                dataType: "json",
                async: false,
                success: function (callback) {
                    if (callback) {
                        isexist = true;
                    }
                },
                error: function () { }
            });  // end $.ajax
            return isexist;
        }
    </script>
}
<div id="container">
    <div id="bmsidebar">
        <div class="title">
            劳保物品列表</div>
        <div id="ptree" style="overflow-y:scroll;">
        </div>
    </div>
    <div id="bmcontent">
        <div id="title" class="title">
             劳保物品管理</div>
        <table id="ConGoodList" style="display: none;">
        </table>
    </div>
    @Html.HiddenFor(model => model.ID)
    @Html.HiddenFor(model => model.PriorId)
</div>

